<?php
namespace app\app\controller;
use think\Controller;
class Main extends Controller
{
   
    public function index()
    {
        $talk = new Talk();
        $if_login = $talk->check_login();
        if ($if_login == false){
            $this->assign('login_status','never');
        }else {
            $talk = new Talk();
            $never_read_num = $talk->never_read();
            $this->assign('never_read_num',$never_read_num);
            $this->assign('user_name',session('user_name','','app'));
            $this->assign('user_id',session('user_id','','app'));
            $this->assign('login_status','have');
            $this->assign('logout_url',url('index.php/app/talk/logout'));
            $this->assign('personal_url',url('index.php/app/main/personal_set'));
        }
        $img_list = db('mainimg')->select();
        $announcement_list = db('announcement')->field(['id','announcement','has_content','descr'])->order('created','desc')->select();
        $this->assign('announcement_list',$announcement_list);
        $index_content_url = url('announcement_content');
        $this->assign('announce_cotent_url',$index_content_url);
        $this->assign('img_list',$img_list);
        $this->assign('title','主页');
        $this->assign('active','main');
        return $this->fetch('index');
        
    }
    
    public function announcement_content()
    {
    	$announcement_id = trim(input('get.announcement_id'));
    	$announcement = db('announcement')->where('id',$announcement_id)->find();
    	$this->assign('announcement',$announcement);
    	$this->assign('title','-公告详情');
    	return $this->fetch('announcement_detail');
    }
    
    
    public function personal_set()
    {
        $user_id = input('get.user_id');
        if(session('role','','app') == 'admin'){
            $user_info = db('user')->where('id',$user_id)->find();
        }elseif (session('role','','app')=='ouser') {
            $user_info = db('ouser')->where('id',$user_id)->find();
        }
        /*
        $user_info = db('user')->where('id',$user_id)->find();
        if ($user_info == false){
            $user_info = db('ouser')->where('id',$user_id)->find();
        }
        */
        $talk = new Talk();
        $if_login = $talk->check_login();
        if ($if_login == false){
            $this->assign('login_status','never');
        }else {
            $talk = new Talk();
            $never_read_num = $talk->never_read();
            $this->assign('never_read_num',$never_read_num);
            $this->assign('user_name',session('user_name','','app'));
            $this->assign('user_id',session('user_id','','app'));
            $this->assign('login_status','have');
            $this->assign('logout_url',url('index.php/app/talk/logout'));
            $this->assign('personal_url',url('index.php/app/main/personal_set'));
        }
        $this->assign('title','个人设置');
        $this->assign('active','personal_set');
        $this->assign('user_info',$user_info);
        return $this->fetch('personal_set');
    }
    
    public function save_user()
    {
        $user_id = input('get.user_id');
        if(session('role','','app') == 'admin'){
            $user_info = db('user')->where('id',$user_id)->find();
        }elseif (session('role','','app')=='ouser') {
            $user_info = db('ouser')->where('id',$user_id)->find();
        }
        $this->assign('title','用户资料修改');
        $this->assign('user_info',$user_info);
        return $this->fetch('save_user');
    }
    
    public function save_user_action()
    {
        $result = array('err' => '1','content' => '');
        $user_info['id'] = input('post.user_id');
        $user_info['user_name'] = input('post.user_name');
        $user_type = input('post.usertype');
        $user_info['password']= input('post.password');
        $rules = [
            'user_name' => 'require|max:20',
            'password' => 'require|max:20'
        ];
        $message = [
            'user_name.require' => '用户名不能为空！',
            'user_name.max' => '用户名的字数请控制在20以内！',
            'password.require' => '密码不能为空！',
            'password.max' => '密码的字数请控制在20以内！'
        ];
        $check_user_info = $this->validate($user_info,$rules,$message);
        if ($check_user_info != 'true'){
            $result['content'] = $check_user_info;
            return $result;
        }
        $user_info['password'] = md5($user_info['password']);
        if ($user_type == 'ordinary'){
            $save_user = db('ouser')->where('id',$user_info['id'])->update($user_info);
        }elseif ($user_type == 'admin'){
            $save_user = db('user')->where('id',$user_info['id'])->update($user_info);
        }
        if ($save_user == false){
            $result['content'] = '修改失败！';
            return $result;
        }
        $result['err'] = '0';
        $result['content'] = '修改成功！';
        return $result;
    }
    
    public function get_warehouse_list()
    {
    	$result = array('err' => '1','content' => '','data' => ['warehouse' => array()] );
    	$warehouse_list = db('warehouse')->field(['id','warehouse'])->select();
    	if ($warehouse_list == false || empty($warehouse_list)){
    		$result['content'] = '获取仓库列表失败！';
    		return $result;
    	}
    	$result['err'] = '0';
    	$result['content'] = '获取仓库列表成功！';
    	$result['data']['warehouse'] = $warehouse_list;
    	return $result;
    }
    
    public function register()
    {
    	$result = array('err' => '1','content' => '');
    	$ouser_info = input('post.');
    	$rules = [
    			'user_name' => 'require|max:40',
    			'password' => 'require|max:20',
    			'warehouse_id' => 'require|number'
    			
    	];
    	$message = [
    			'user_name.require' => '用户名必填！',
    			'user_name.max' => '用户名的字数请控制在20以内！',
    			'password.require' => '密码必填！',
    			'password.max' => '密码的字数请控制在20以内！',
    			'warehouse_id.require' => '参数缺失！',
    			'warehouse_id.number' => '参数错误！'	
    	];
    
    	$check_ouser_info = $this->validate($ouser_info,$rules,$message);
    	if ($check_ouser_info != 'true'){
    		$result['content'] = $check_ouser_info;
    		return $result;
    	}
    	 
    	$model = model('Main');
    	$insert_ouser_tmp = $model->register_ouser($ouser_info);
    	 
    	if ($insert_ouser_tmp['err'] == '1'){
    		$result['content'] = '注册失败！'.$insert_ouser_tmp['content'];
    		return $result;
    	}
    	$result['err'] = '0';
    	$result['content'] = '注册成功！注册ID为：'.$insert_ouser_tmp['content'];
    	return $result;
    	
    }
}


